package edu.must.uh.servlet;

import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import edu.must.uh.bean.DeployQueryResultBean;
import edu.must.uh.impl.AttendancdOTTimeImpl;
import edu.must.uh.util.R;
import edu.must.uh.util.ServletUtils;
import edu.must.uh.util.Tools;
import edu.must.uh.util.ToolsOfString;

@WebServlet("/AttendancdOTTimeServlet")
public class AttendancdOTTimeServlet extends BaseServlet {
	
    private static final long serialVersionUID = 12197442526341123L;
    private static Logger logger = Logger.getLogger(AttendancdOTTimeServlet.class);  
    AttendancdOTTimeImpl attendancdOTTimeImpl = new AttendancdOTTimeImpl();
    
    public Object getList(HttpServletRequest request, HttpServletResponse response) throws Exception {      
     
    	
    	attendancdOTTimeImpl._PageBean = super.pageBean;
        Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
        String sqlString = "";
        if(searchcondition !=null && !searchcondition.isEmpty() && searchcondition.get("ATTEND_DATE") !=null) {
        	String ATTEND_DATE =ToolsOfString.NulltoEmpty(searchcondition.get("ATTEND_DATE")) ;
        	sqlString =" AND DATE_FORMAT(ATTEND_DATE,'%Y-%m') = '"+ATTEND_DATE+"'  ";	
        	searchcondition.put("ATTEND_DATE", "");
        }
        DeployQueryResultBean resList = attendancdOTTimeImpl.getLimitlist(searchcondition, sqlString +" ORDER BY ATTEND_DATE desc,START_TIME ");                 
        for (Map<String, Object> map : resList.getResultList()) {
                   
            ServletUtils.createDateHelper(map,"ATTEND_DATE"); 
        }
        
        return R.succ(resList.getResultList(),resList.getTotal());         
    }
    

    
    public Object getOneData(HttpServletRequest request, HttpServletResponse response) throws Exception { 
    	String keyValue =ToolsOfString.NulltoEmpty(request.getParameter("ID")); 
    	String sqlString = "SELECT  UserId, ATTEND_DATE,START_TIME,END_TIME,HOURS,REMARK FROM attendance_ottime Where DELETEMARK = 0  ";
    	sqlString = sqlString + " AND ID = '"+keyValue+"'";
        Map<String,Object>  map = attendancdOTTimeImpl.getMap(sqlString); 
        ServletUtils.createDateHelper(map,"ATTEND_DATE"); 
        return R.succ(map);         
    }
    
    public Object delete(HttpServletRequest request, HttpServletResponse response) throws Exception {      
            
        String keyValue =ToolsOfString.NulltoEmpty(request.getParameter("ID")); 
        Integer resInteger = attendancdOTTimeImpl.delete(keyValue);
        return R.succ(resInteger);         
    }
    
  public Object save(HttpServletRequest request, HttpServletResponse response) throws Exception {  
      		
		String sql = "";
		Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
	    String ID =ToolsOfString.NulltoEmpty(searchcondition.get("ID"));
        String ATTEND_DATE =ToolsOfString.NulltoEmpty(searchcondition.get("ATTEND_DATE")); 
        String UserId =ToolsOfString.NulltoEmpty(searchcondition.get("UserId"));
        String START_TIME =ToolsOfString.NulltoEmpty(searchcondition.get("START_TIME"));
        String END_TIME =ToolsOfString.NulltoEmpty(searchcondition.get("END_TIME"));
        String HOURS =ToolsOfString.NulltoEmpty(searchcondition.get("HOURS"));
        
        
        String REMARK =ToolsOfString.NulltoEmpty(searchcondition.get("REMARK"));
        REMARK =  java.net.URLDecoder.decode(REMARK, "UTF-8");
       
        if(Tools.notEmpty(ID)) {
        	sql = "UPDATE attendance_ottime  SET ATTEND_DATE = '"+ATTEND_DATE+"',START_TIME = '"+START_TIME+"',END_TIME = '" +END_TIME+"',HOURS = '" +HOURS+"',REMARK = '" +REMARK+"'";
        	sql = sql + " WHERE ID ='"+ID+"'"; 
        }else {
            ID = Tools.getUUID32();
        	sql = "INSERT INTO attendance_ottime(ID,UserId,ATTEND_DATE,START_TIME,END_TIME,HOURS,REMARK)VALUES";
        	sql = sql + " ('"+ID+"','"+UserId+"','"+ATTEND_DATE+"','"+START_TIME+"','"+END_TIME+"','"+HOURS+"','"+REMARK+"')"; 
        }
        Integer resInteger = attendancdOTTimeImpl.save(sql);
        if(resInteger == 1) {
        	   return R.succ(); 
        }
        return R.fail();
       
    }
    
    
    
   
}
